<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;

/**
 * 文档模型控制器
 * 文档模型列表和详情
 */
class ArticleController extends HomeController {

    /* 文档模型频道页 */
	public function index(){
		/* 分类信息 */
		$category = $this->category();

		//频道页只显示模板，默认不读取任何内容
		//内容可以通过模板标签自行定制
		/* 模板赋值并渲染模板 */

		$this->assign('category', $category);
		$this->display($category['template_index']);
	}

	/* 文档模型列表页 */
	public function lists($p = 1){
		/* 分类信息 */
		$category = $this->category();

		/* 获取当前分类列表 */
		$Document = D('Document');
		$list = $Document->page($p, $category['list_row'])->lists($category['id']);
        $lists = $Document
            ->alias('D')
            ->join('left join __DOCUMENT_ARTICLE__ as DA on D.id = DA.id')
            ->where(array('D.category_id'=>144,'D.status'=>1))
            ->order('D.id desc')
            ->page($p, $category['list_row'])
            ->select();
		if(false === $list){
			$this->error(L('failed_data'));
		}
        //分页
        $page = new \Think\Page(get_list_count($category['id']),$category['list_row']);
        /*$page->setConfig('prev',L('page_prev'));
        $page->setConfig('next',L('page_next'));
        $page->setConfig('first',L('page_first'));
        $page->setConfig('last',L('page_last'));*/
//        var_dump($lists);die;

		$this->assign('pages',$page->show());
		/* 模板赋值并渲染模板 */
		$this->assign('category', $category);
		$this->assign('list', $list);
        $this->assign('lists', $lists);
		$this->display($category['template_lists']);
	}

	/* 文档模型详情页 */
	public function detail($id = 0, $p = 1){
		/* 标识正确性检测 */
		if(!($id)){
			$this->error(L('article_id_error'));
		}

		/* 页码检测 */
		$p = intval($p);
		$p = empty($p) ? 1 : $p;

		/* 获取详细信息 */
		$Document = D('Document');
		$info = $Document->detail($id);

		if(!$info){
			$this->error($Document->getError());
		}

		/* 分类信息 */
		$category = $this->category($info['category_id']);

		/* 获取模板 */
		if(!empty($info['template'])){//已定制模板
			$tmpl = $info['template'];
		} elseif (!empty($category['template_detail'])){ //分类已定制模板
			$tmpl = $category['template_detail'];
		} else { //使用默认模板
			$tmpl = 'Article/'. get_document_model($info['model_id'],'name') .'/detail';
		}

		/* 更新浏览数 */
		$map = array('id' => $info['id']);
		$Document->where($map)->setInc('view');

		/* 模板赋值并渲染模板 */
		$this->assign('category', $category);
		$this->assign('info', $info);
		$this->assign('page', $p); //页码
		$this->display($tmpl);
	}

	/* 文档分类检测 */
	private function category($id = 0){
		/* 标识正确性检测 */
		$id = $id ? $id : I('get.category', 0);
		if(empty($id)){
			$this->error(L('category_is_empty'));
		}

        $id = getLoaclName($id,false);

		/* 获取分类信息 */
		$category = D('Category')->info($id);
		if($category && 1 == $category['status']){
			switch ($category['display']) {
				case 0:
					$this->error(L('category_display'));
					break;
				//TODO: 更多分类显示状态判断
				default:
					return $category;
			}
		} else {
			$this->error(L('category_exist'));
		}
	}
    public function message(){
	    if(IS_POST){
	        $data = array(
	            'name' => I('name',''),
                'tel'  => I('tel',''),
                'qq'   => I('qq',''),
                'weixin' => I('wx',''),
                'content' => I('nr'),
                'create_time' => time()
            );
	        if(I('name') == ''){
	            $this->error('联系人不能为空');
            }else if(I('tel') == ''){
                $this->error('联系电话不能为空');
            }else if(I('nr') == ''){
                $this->error('内容不能为空');
            }
            $result = M('Message')->add($data);
            if($result){
                $this->success('提交成功');
            }else{
                $this->error('提交失败');
            }
        }
    }
    public function ajax(){
        if(IS_AJAX){
            $id = I('post.ca_id');
            $info = M('Document')
                ->alias('D')
                ->join('left join __DOCUMENT_ARTICLE__ as DA on D.id = DA.id')
                ->join('left join __PICTURE__ as P on D.cover_id = P.id')
                ->where(array('D.id'=>$id))
                ->find();
            $this->ajaxReturn($info);

        }
    }
    public function product(){
        if(IS_AJAX){
            $id = I('post.cate');
            $data = M('Document')
                ->alias('D')
                ->join('left join __DOCUMENT_ARTICLE__ as DA on D.id = DA.id')
//                ->join('left join __PICTURE__ as P on D.cover_id = P.id')
//                ->join('left join __CATEGORY__ as CA ')
                ->where(array('D.category_id'=>$id,'D.status'=>1,'D.position'=>4))
                ->order('D.id desc')
                ->limit(8)
                ->select();
           $this->assign('data',$data);
           $html =   $this->fetch('Index/show');
           $this->ajaxReturn($html);
//            echo json_encode($data);
//            $this->ajaxReturn($data);
        }
    }
}
